package action;

import entity.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;

import service.tUserService;

public class SearchtUserAction {
	
	private String userName;
	private int pageSize;
	private int page;    //第几页
    
    private PageBean pageBean;
	
	private tUserService tuserService;
	
	@SuppressWarnings("unchecked")
	public String search() throws IOException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		//HttpSession session = ServletActionContext.getRequest().getSession();
		List<tUser> lists = new ArrayList<tUser>();
		JSONArray jsonArray = new JSONArray();
		this.pageBean = tuserService.searchuserName(userName, 15, page);
		List<tUser> list = pageBean.getList();
		
		System.out.println(list);
		//tUser tuser = new tUser();
		for(tUser tuser: list){
			JSONObject t = new JSONObject();
			t.put("userId", tuser.getUserId());
			t.put("userName", tuser.getUserName());
			t.put("trueName", tuser.getTrueName());
			t.put("email", tuser.getEmail());
			
			t.put("mobilephone", tuser.getMobilePhone());
			t.put("phone", tuser.getPhone());
			jsonArray.add(t);
			
			System.out.println(t);
			
			
		}
		//System.out.println(jsonArray.toString());
	
		//lists = jsonArray.toString();
		
		request.setAttribute("lists",list);
		
		//System.out.println("11111111111111111111111111111");
		return "success";
	}

	public tUserService getTuserService() {
		return tuserService;
	}

	public void setTuserService(tUserService tuserService) {
		this.tuserService = tuserService;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	
	

}
